<template>
  <div>
      <h3 class="titleH3">基本信息</h3>
      <FormPanel
        ref="FormPanel"
        :list="FormPanelList"
        :cols="3"
        :formType="type"
        :labelWidth="120"
        :isSubmitBtn='false'
        :collapse="false"
      ></FormPanel>
      <div style="text-align: center;margin: 20px 0 20px 0;">
          <el-button size="small" type=""  @click="closeHandler">返回</el-button>
          <el-button size="small" type="primary" v-show="type == 'show'?false:true"  @click="saveHandle">保存</el-button>
      </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import {putSaleInfo} from '@/api/common/org/gms/saleCompanyMaintain';
import { getlistAllData} from "@/api/common/org/dealerCompany";
import * as types from '@/api/common/auth/roleManage/types';
import { IDNumber, phoneAndSolidify,phone,endTime,testText32,testText16,testNumLetter, email, formatDates, checkPassport} from '@/utils/validate'
export default {
  name: 'modal',
  mixins: [dictionary],
  props:['rowData','type','listAllData','isOk'],
  components: {
  },
  data() {
    this.BaseTable = null;
    return {
        FormPanelList: this.createFormPanelList(),
        // listAllData:[],
        province:[],
    };
  },
 /*  watch: {
      isOk(oldval,newval){
        if(oldval){
          
          this.FormPanelList.forEach(ele => {
            this.listAllData.forEach(x=>{
              if(x.regionId == ele.initialValue){
                ele.initialValue=x.regionName
              }
              // 筛选省
              if(x.parentRegionId == null){
                this.province.push({text:x.regionName,value:x.regionId})
              }
            })
          });
          this.FormPanelList[3].itemList=_.uniqBy(this.province,'value')
        }
      }
  }, */
  methods: {
      init(){
          this.FormPanelList.forEach(x=>{
              x.initialValue=this.rowData[x.fieldName]
          })

          if(this.type == 'show'){
            this.FormPanelList.forEach(x=>{
              x.rules=[]
           })
          }

          if(this.listAllData){
          
          this.FormPanelList.forEach(ele => {
            this.listAllData.forEach(x=>{
              if(x.regionId == ele.initialValue){
                ele.initialValue=x.regionName
              }
              // 筛选省
              if(x.parentRegionId == null){
                this.province.push({text:x.regionName,value:x.regionId})
              }
            })
          });
          this.FormPanelList[3].itemList=_.uniqBy(this.province,'value')
        }
      },
      createFormPanelList() {
      return [
        {
          type: 'INPUT',
          label: '公司代码',
          fieldName: 'companyCode',
          placeholder: '',
          style: { width: '240px' },
          disabled:true,
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '公司名称',
          fieldName: 'companyNameCn',
          placeholder: '',
          style: { width: '240px' },
          maxlength:32,
          // validator:testText32
          rules: [{ required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 32, message: '长度在 1 到 32 个字符', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '公司简称',
          fieldName: 'companyShortNameCn',
          placeholder: '',
          style: { width: '240px' },
          // rules: [{ required: true, validator:testText16, trigger: 'blur' }]
          maxlength:16,
          // validator:testText32
          rules: [{ required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 16, message: '长度在 1 到 16 个字符', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '省',
          fieldName: 'provinceId',
          placeholder: '',
          style: { width: '240px' },
          itemList: [],
          change:val=>{
            let city=[]
            if(val == 3662){
              this.FormPanelList[4].itemList=[{text:"台湾",value:3662}]
              this.FormPanelList[5].itemList=[{text:"台湾",value:3662}]
            }else if(val == 3663){
              this.FormPanelList[4].itemList=[{text:"香港",value:3663}]
              this.FormPanelList[5].itemList=[{text:"香港",value:3663}]
            }else if(val == 3664){
              this.FormPanelList[4].itemList=[{text:"澳门",value:3664}]
              this.FormPanelList[5].itemList=[{text:"澳门",value:3664}]
            }
            this.FormPanelList[4].initialValue=''
            this.FormPanelList[5].initialValue=''
            this.listAllData.forEach(x=>{
              // 筛选省
              if(x.parentRegionId == val){
                city.push({text:x.regionName,value:x.regionId})
                this.FormPanelList[4].itemList=_.uniqBy(city,'value')
              }
            })
          },
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '市',
          fieldName: 'cityId',
          placeholder: '',
          style: { width: '240px' },
          itemList: [],
          change:val=>{
            let county=[]
            this.FormPanelList[5].initialValue=''
            this.listAllData.forEach(x=>{
              // 筛选省
              if(x.parentRegionId == val){
                county.push({text:x.regionName,value:x.regionId})
                this.FormPanelList[5].itemList=_.uniqBy(county,'value')
              }
            })
          },
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '区/县',
          fieldName: 'county',
          placeholder: '',
          style: { width: '240px' },
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '地址',
          fieldName: 'addressZh',
          placeholder: '',
          style: { width: '240px' },
          maxlength:32,
          // rules: [{ required: true, validator:testText32, trigger: 'blur' }]
          rules: [{ required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 32, message: '长度在 1 到 32 个字符', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '联系电话',
          fieldName: 'phone',
          placeholder: '',
          style: { width: '240px' },
          maxlength:32,
          rules: [{ required: false, validator:phoneAndSolidify, trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '传真',
          fieldName: 'fax',
          placeholder: '',
          style: { width: '240px' },
        }
      ];
    },
    
    getCookie(){
        let allCookie=document.cookie.split(';')
        allCookie.forEach(x=>{
          if(x.indexOf('dataType') > -1 ){
            this.dataType=x.split('=')[1]
            }
        })
        //判断是否为车厂10461003
        console.log(this.dataType,"dataType")
        if(this.dataType == 10461003){
          console.log(this.dataType,"车厂")
        }
      },
    // 新增
    /* addTable(){
        
    }, */
    // 编辑
    editFun(row) {
      this.rowData=row
    },
    
    // 保存
    saveHandle(){
      let saveData = this.$refs.FormPanel.SUBMIT_FORM()
      let params={
          ...saveData
      }
        // this.FormPanelList.forEach(ele=>{
        //   Object.keys(params).forEach(x=>{
        //     if(x == ele.fieldName){
        //         params[x] =ele.initialValue
        //     }
        //   })
        // })
        console.log(this.FormPanelList,"456",this.rowData)
        let testText = /^[\u4E00-\u9FA5]{0,}$/;
        if (!testText.test(this.FormPanelList.find(item=>item.fieldName == "provinceId").initialValue) ||!testText.test(this.FormPanelList.find(item=>item.fieldName == "cityId").initialValue) ||!testText.test(this.FormPanelList.find(item=>item.fieldName == "county").initialValue)) {
          params.provinceId=this.FormPanelList.find(item=>item.fieldName == "provinceId").initialValue
          params.cityId=this.FormPanelList.find(item=>item.fieldName == "cityId").initialValue
          params.county=this.FormPanelList.find(item=>item.fieldName == "county").initialValue
        }else{
          params.provinceId=this.rowData.provinceId
          params.cityId=this.rowData.cityId
          params.county=this.rowData.county
        }
        
        params.companyId=this.rowData.companyId
        var g =  /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/
        if(params){
            if(params.fax){
              if (!g.test(params.fax)) {
                this.$message({
                  message: '请输入正确得传真值',
                  type: 'warning'
                });
              }else{
                if(params.fax.length >32 || params.companyNameCn.length >32 || params.companyShortNameCn.length >16 || params.addressZh.length >32 || params.phone.length >11){
                  this.$message({
                    message: '请输入正确内容',
                    type: 'warning'
                  });
                }else{
                    putSaleInfo(params).then(res=>{
                    if(res.resultCode == 200){
                      this.closeHandler('refresh')
                      this.$message({
                        message: '保存成功',
                        type: 'success'
                      });
                    }
                  })
                }
              }
            }else{
              if(params.companyNameCn.length >32 || params.companyShortNameCn.length >16 || params.addressZh.length >32 || params.phone.length >11){
                this.$message({
                  message: '请输入正确内容',
                  type: 'warning'
                });
              }else{
                  putSaleInfo(params).then(res=>{
                  if(res.resultCode == 200){
                    this.closeHandler('refresh')
                    this.$message({
                      message: '保存成功',
                      type: 'success'
                    });
                  }
                })
              }
            }
        }
    },
    closeHandler(type) {
        this.$emit('close',"")
    }
  },
  async mounted () {
    this.getCookie()
    this.init()
    // await getlistAllData().then(res=>{
    //   this.listAllData=res.data
    //   this.isOk=true
    // })
    
  },
};
</script>

<style lang="less" scoped>
.titleH3{
    height: 30px;
    line-height: 30px;
    margin-bottom: 5px;
    background: #eee;
    text-indent: 5px;
}
</style>
